# @Time: 2022/10/20
# @Author: HeYuanqing
import os
import csv


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# 读取csv文件
def read_csv(file_path):
    data_list = []
    with open(file_path) as f:
        f_csv = csv.reader(f)
        # 获取数据头
        headers = next(f_csv)
        data_list.append(headers)
        # 循环获取每一行
        for row in f_csv:
            data_list.append(row)
    print("csv文件：" + file_path + " 读取成功！")
    return data_list


# 写csv文件
def write_csv(dir_name, file_name, file_data):
    if not os.path.isdir(dir_name):
        os.makedirs(dir_name)
    file_path = dir_name + "\\" + file_name
    height = len(file_data)
    if height < 0:
        print("写csv文件失败！")
        return
    width = len(file_data[0])
    if width < 0:
        print("写csv文件失败！")
        return
    with open(file_path, 'w', newline='', encoding='utf-8') as file_csv:
        writer_csv = csv.writer(file_csv)
        # 按行写入
        writer_csv.writerows(file_data)
    file_csv.close()

    print("csv文件：" + file_path + " 写入成功！")

# 读取json文件
def read_json(file_path):
    import json
    f = open(file_path, 'r')
    data = json.load(f)
    f.close()
    return data


# 获取文件夹下的文件名
def get_filename(rootpath):
    files = []
    if os.path.exists(rootpath):
        for file_name in os.listdir(rootpath):
            files.append(file_name)
    else:
        print("路径：" + rootpath + " 不存在！")

    return files


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
